﻿using BoldSeas.Framework.Ado.SqlClient;
using BoldSeas.Framework.Common.Helper;
using BoldSeas.Framework.Log;
using FCA.Common;
using FCA.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DAl
{
    public class UserDal
    {

        #region 获取所有销售人员的信息
        /// <summary>
        /// 获取所有销售人员的信息
        /// </summary>
        /// <returns></returns>
        public static List<FCA_DealerUser> GetDealerUser()
        {
            try
            {

                string strSql = @"   select
                                        us.ID, 
                                        convert(varchar(3),UserRole) UserRole ,
                                        DealerUserID,
                                        DealerCode,
                                        DMSRoleID,
                                        DMSRoleName,
                                        LoginName,
                                        PassWord,
                                        UserName, 
                                        UserPhone,
                                        IsShowExplain, 
                                        Email,
                                        IsDCCView,
                                        us.CreateDate,
                                        UserStatus,
                                        usro.RoleName
                                        from DealerUser us
                                        left join SYS_DealerUserRole usro on usro.ID=us.UserRole
                                        where UserStatus='12101001'";
                DataTable table = SqlHelper.ExecuteDatatable(ConfigurationHelper.FCAuserRead, CommandType.Text, strSql);
                List<FCA_DealerUser> myList = null;
                if (table != null && table.Rows.Count > 0)
                {
                    myList = ModelConvertHelper<FCA_DealerUser>.ConvertToModelList(table);
                }
                return myList;
            }
            catch (Exception ex)
            {
                Log4net.Error(MethodBase.GetCurrentMethod(), ex);
                return new List<FCA_DealerUser>();
            }
        }
        #endregion

        #region 插入销售人员的信息
        /// <summary>
        /// 插入销售人员的信息
        /// </summary>
        /// <returns></returns>
        public static string InsertUser(string strSql)
        {
            Log4net.Info("插入：" + strSql);
            string Result = " Failure";
            try
            {
                int id = SqlHelper.ExecuteNonQuery(ConfigurationHelper.FCAuserWrite, CommandType.Text, strSql);
                if (id > 0)
                {
                    Result = "Success";
                }
                return Result;
            }
            catch (Exception ex)
            {
                Log4net.Error(MethodBase.GetCurrentMethod(), ex);
                return Result;
            }
        }
        #endregion
        #region 更新
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static string UpUser(string sql)
        {

            string Result = " Failure";
            Log4net.Info("更新：" + sql);
            try
            {
                int id = SqlHelper.ExecuteNonQuery(ConfigurationHelper.FCAuserWrite, CommandType.Text, sql);
                if (id > 0)
                {
                    Result = "Success";
                }
                return Result;
            }
            catch (Exception ex)
            {
                Log4net.Error(MethodBase.GetCurrentMethod(), ex);
                return Result;
            }
        }
        #endregion

        #region 插入临时表销售人员的信息
        /// <summary>
        /// 插入临时表销售人员的信息
        /// </summary>
        /// <returns></returns>
        public static string InsertUserTemp(string strSql)
        {
            string Result = " Failure";
            Log4net.Info("插入临时表" + strSql);
            try
            {
                int id = SqlHelper.ExecuteNonQuery(ConfigurationHelper.TempChryslerFACAppWrite, CommandType.Text, strSql);
                if (id > 0)
                {
                    Result = "Success";
                }
                return Result;
            }
            catch (Exception ex)
            {
                Log4net.Error(MethodBase.GetCurrentMethod(), ex);
                return Result;
            }
        }
        #endregion
        #region 更新临时表
        /// <summary>
        /// 更新临时表
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static string UpUserTemp(string sql)
        {
            Log4net.Info("更新临时表" + sql);             
            string Result = " Failure";
            try
            {
                int id = SqlHelper.ExecuteNonQuery(ConfigurationHelper.TempChryslerFACAppWrite, CommandType.Text, sql);
                if (id > 0)
                {
                    Result = "Success";
                }
                return Result;
            }
            catch (Exception ex)
            {
                Log4net.Error(MethodBase.GetCurrentMethod(), ex);
                return Result;
            }
        }
        #endregion

    }
}
